<?php

class Mails {
    
    private $gmail = "";
    private $gmail_pwd = "";
    private $from = "";
    private $fromname = "";
    private $host = "";
    private $host_acc = "";
    private $host_pwd = "";
    private $host_port = "25";
    private $host_auth = true;
    private $host_ssl = false;
    public $flag = true;
    private $faxtoserver = "";
    private $fax_account = "";
    private $fax_pwd = "";

    public function __construct() {
        
    }

    public function getFaxToServer() {
        return $this->faxtoserver;
    }

    public function getFaxAccount() {
        return $this->fax_account;
    }

    public function getFaxPWD() {
        return $this->fax_pwd;
    }

    public function setFrom($from) {
        $this->from = $from;
    }

    public function setFromname($fromname) {
        $this->fromname = $fromname;
    }

    public function sendMail($to, $subject, $data, $charset = null, $isCC = false) {
        include_once ('plugin/phpmailer/class.phpmailer.php');
        include_once ('plugin/phpmailer/class.smtp.php');
        $Config = Config::getInstance();
        $getSMTP_arr = $Config->getSMTP();
        //include("class.phpmailer.php");
        $from = $getSMTP_arr["sender_email"];
        $fromname = $getSMTP_arr["sender_name"];
        $host = $getSMTP_arr["Host"];
        //echo $host;
        //die;
        $host_acc = $getSMTP_arr["Username"];
        $host_pwd = $getSMTP_arr["Password"];
        $host_port = $getSMTP_arr["Port"];
        $host_auth = true;
        $host_ssl = $getSMTP_arr["isSSL"];
        $charset = empty($charset) ? "UTF-8" : $charset;
        $fromname = $charset == "UTF-8" ? $fromname : iconv("UTF-8", "big5", $fromname);
        $mail = new PHPMailer();
        $mail->IsSMTP();

        $mail->SMTPAuth = $host_auth;

        if ($host_ssl)
            $mail->SMTPSecure = "ssl";

        $mail->Host = $host;
        $mail->Port = $host_port;
        $mail->Username = $host_acc; //設定驗證帳號 
        $mail->Password = $host_pwd; //設定驗證密碼 
        $mail->From = $host_acc; //設定寄件者信箱 
        $mail->FromName = $fromname; //設定寄件者姓名 
        //設定收件者 
        $mail->AddAddress($to);
         
        //設定信件字元編碼 
        $mail->CharSet = $charset;

        if ($isCC) {
            //cc_mail
            $toCC = explode(',', $getSMTP_arr['cc_mail']);
            foreach ($toCC as $key => $value) {
                $mail->AddCC($value, '密件收件者');
            }
        }

        //設置郵件格式為HTML 
        $mail->IsHTML(true);

        //郵件主題 
        $mail->Subject = $subject;
        //$data = $body;
        //郵件內容 
        $mail->Body = $data;
        //附加內容 
        // $mail->AltBody = '這是附加的信件內容';
        //寄送郵件 

        if (!$mail->Send()) {
            $this->flag = false;
            return "郵件無法順利寄出! Mailer Error: " . $mail->ErrorInfo;
        }else{
            return 'ok';
	}
    }

}

?>